<template>
  <!-- 层叠过渡效果 -->
  <transition name="el-zoom-in-center">
    <div class="jsbos-preview-main flow-form-main">
      <div class="jsbos-common-page-header">
        <!-- 页头 -->
        <el-page-header @back="goBack" content="添加试题" />
        <!-- 确定/取消 -->
        <div class="options">
          <el-button
            type="primary"
            @click="dataFormSubmit()"
            :loading="btnLoading"
          >
            {{ $t("common.confirmButton") }}</el-button
          >
          <el-button @click="goBack()">{{
            $t("common.cancelButton")
          }}</el-button>
        </div>
      </div>
      <!-- 接收人记录 -->
      <question ref="questionRef" :questions="dataForm"></question>
    </div>
  </transition>
</template>

<script>
import question from "./question";
import { Create, batchCreate } from "@/views/exam/api/question";

export default {
  components: { question },
  props: {
    menuId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      btnLoading: false,
      loading: false,
      dataForm: [],
    };
  },
  watch: {},
  methods: {
    dataFormSubmit() {
      var _dataList = this.dataList();

      this.btnLoading = true;
      console.log("batchCreate", _dataList);
      batchCreate(_dataList)
        .then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.btnLoading = false;
              this.$emit("close", true);
            },
          });
        })
        .catch(() => {
          this.btnLoading = false;
        });
    },
    dataList() {
      console.log(this.$refs.questionRef.questionList);
      const _folderId = this.folderId;
      this.dataForm = this.$refs.questionRef.questionList;
      var _dataList = JSON.parse(JSON.stringify(this.dataForm));
      _dataList.forEach((_data, idx) => {
        _data.folderId = _folderId;
        _data.titimgs = JSON.stringify(_data.titimgs);
        _data.options = JSON.stringify(_data.options);
        _data.opimgs = JSON.stringify(_data.opimgs);
        _data.answers = JSON.stringify(_data.answers);
        _data.labels = JSON.stringify(_data.labels);
        _data.analysisimgs = JSON.stringify(_data.analysisimgs);
      });
      console.log("_dataList", _dataList);
      return _dataList;
    },
    /** 返回 */
    goBack() {
      this.$emit("close", false);
    },
    /**问题列表初始化初始化 */
    init(folderId) {
      this.folderId = folderId;
    },
  },
};
</script>
<style lang="scss" scoped></style>
